package May._03.homework.work2;

import org.junit.Test;

import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Transaction> transactions = TransactionRecord.transactions;
        String str = transactions.stream()
                .map(a -> a.getTrader().getName())
                .distinct()
                .sorted(String::compareTo)
                .reduce("", (a , b) -> {
                    return a+" "+b;
                });
        System.out.println(str);
    }
    @Test
    public void test2011(){
        List<Transaction> result = TransactionRecord.transactions.stream()
                .filter(new Predicate<Transaction>() {
                    @Override
                    public boolean test(Transaction transaction) {
                        return transaction.getYear() == 2011;
                    }
                })
                .sorted(new Comparator<Transaction>() {
                    @Override
                    public int compare(Transaction o1, Transaction o2) {
                        return o1.getValue() - o2.getValue();
                    }
                })
                .collect(Collectors.toList());
        result.forEach(new Consumer<Transaction>() {
            @Override
            public void accept(Transaction transaction) {
                System.out.println(transaction);
            }
        });
    }

    @Test
    public void testCity(){
        List<String> result1 = TransactionRecord.transactions.stream()
                .map(new Function<Transaction, Trader>() {
                    @Override
                    public Trader apply(Transaction transaction) {
                        return transaction.getTrader();
                    }
                })
                .map(new Function<Trader, String>() {
                    @Override
                    public String apply(Trader trader) {
                        return trader.getCity();
                    }
                })
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result1);

    }

    @Test
    public void testBeijing(){
        List<Trader> result2 = TransactionRecord.transactions.stream()
                .map(new Function<Transaction, Trader>() {
                    @Override
                    public Trader apply(Transaction transaction) {
                        if (transaction.getTrader().getCity().equals("beijing")) {
                            return transaction.getTrader();
                        }
                        return null;
                    }
                })
                .distinct()
                .collect(Collectors.toList());
        result2.forEach(new Consumer<Trader>() {
            @Override
            public void accept(Trader trader) {
                if(trader!=null){
                    System.out.println(trader);
                }
            }
        });
    }
}
